require('../../../lib/localResizeIMG/dist/lrz.all.bundle.js');
define('upLoadImg',['common'],function(utils){
	'use strict';

	var upLoadImg = {
			showPic:function(img,input){
				var pic = input.get(0).files[0];
				img.find('.pic').prop("src" , window.URL.createObjectURL(pic));
				//console.log(pic)
				return pic;
			},
			uploadFile:function(filesList){
				var formData = new FormData();
				$.each(filesList,function(k,p){
					if(p!==undefined && p!==false){
						formData.append("imgStr",p);
					}
				})
				return formData;
			},
			ajax:function(formData,onprogress,suc){
				var _this = this;
				$('.upLoadimg').css({'height':'100%'});
				var url = utils.getWebUrl('upload/pic_min');
				var onprogress = onprogress;
				$.ajax({
				   type: "POST",
				   url: url,
				   data: formData,
				   headers: {
		               'authorization': 'Bearer '+utils.userToken()
		           },
		           async: true,
				   processData : false, 
				   //必须false才会自动加上正确的Content-Type 
				   contentType : false , 
				   xhr: function(){
					    var xhr = $.ajaxSettings.xhr();
					    if(onprogress && xhr.upload) {
						     xhr.upload.addEventListener("progress" , onprogress, false);
						     return xhr;
					    }
				   },
				   beforeSend:function(res){
				   		$('.Pop_loading').show();
				   },
				   complete:function(res){
				   		$('.Pop_loading').hide();
				   },
				   success:function(res){
					   	if(res.code === "10000"){
					   		suc(res);
	                    }else{
	                        utils.appAlert(res.msg);
	                    }
				   },
				   error: function(res){
				   	   $('.Pop_loading').hide();
	                   utils.appAlert('系统出现了错误,系统管理员正在修复');
	               }
				});
			}
	}
	return upLoadImg;
})